<?php
$this->breadcrumbs=array(
	'Clients'=>array('index'),
	$model->Names,
);

if(Yii::app()->user->getState('role')=="employee"){
$this->menu=array(
	array('label'=>'List Client', 'url'=>array('index')'url'=>array('index'),'visible'=>Yii::app()->user->checkAccess('client.index')),
	array('label'=>'Create Client', 'url'=>array('create')'url'=>array('index'),'visible'=>Yii::app()->user->checkAccess('client.create')),
	array('label'=>'Update Client', 'url'=>array('update', 'id'=>$model->id)),
	array('label'=>'Delete Client', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
	array('label'=>'Manage Client', 'url'=>array('admin')'url'=>array('index'),'visible'=>Yii::app()->user->checkAccess('client.admin')),
);
}
?>

<h1><?php echo $model->getNames(); ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		//'id',
		'client_lastname',
		'client_firstname',
		'client_midname',
		'gender',
		'birthdate',
		'birthplace',
		'phonenum',
		'mobilenum',
		'business_phone',
		'address',
		'zipcode',
		'city_province',
		'marital_status',
		'occupation',
		'monthly_income',
		'isAssured',
		array(
            'label'=>'Referred by',
            'name'=>'employee_id',
            'value'=>$model->employee->Names
         ),
		//'employee_id',
		//'user_id',
	),
)); ?>

<?php $cid= $model->id;?> <!--logged-in user id-->

<?php
/**
SELECT * 
FROM insurance t 
LEFT JOIN insurance_has_payment ip ON ip.insurance_id=t.id
LEFT JOIN insurance_type itype ON i.insurance_type_id=itype.id
WHERE i.client_id='1'
**/
$ins=new CDbCriteria;
$ins->select='*';
$ins->join = 'LEFT JOIN insurance_type itype ON t.insurance_type_id=itype.id';
$ins->condition='t.client_id=:cid';
$ins->params = array(':cid'=>$model->id);
//$ins>order = 'date DESC';
//$cpay->limit = '1';
$insurance=Insurance::model()->findAll($ins);

if (count($insurance) !== 0){?>
<br>

<?php foreach ($insurance as $row) { ?>
<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$row,
	'attributes'=>array(
		//'insuranceType.type_name',
		array(
			'name'=>'insurance_type_id',
			'type'=>'raw',
			'value'=>CHtml::link(CHtml::encode($row->insuranceType->type_name), array('/insurance/view', 'id'=>$row->id)),
		),
	),
)); 
?>
<?php }} ?>

<br/>
<h2>Payments</h2>
<?php
/**
SELECT * 
FROM payment t
LEFT JOIN insurance_has_payment ip ON ip.payment_id=t.id 
LEFT JOIN insurance i ON ip.insurance_id=i.id
LEFT JOIN insurance_type itype ON i.insurance_type_id=itype.id
WHERE i.client_id='{id}'
**/
$cpay=new CDbCriteria;
$cpay->select='*';
$cpay->join = 'LEFT JOIN insurance_has_payment ip ON ip.payment_id=t.id LEFT JOIN insurance i ON ip.insurance_id=i.id LEFT JOIN insurance_type itype ON i.insurance_type_id=itype.id';
$cpay->condition='i.client_id=:cid'; // AND itype.id=:type
$cpay->params = array(':cid'=>$model->id); //, 'type'=>$type
$cpay->order = 'date DESC';
//$cpay->limit = '1';
$payment=Payment::model()->findAll($cpay);

if (count($payment) !== 0){?>
<table>
	<thead> <tr> <th>Payroll Date</th> <th>Payment Amount</th> </tr> </thead>

<?php foreach ($payment as $row) { ?>
	<tr>
		<td><?php echo CHtml::encode(Yii::app()->dateFormatter->formatDateTime($row->date, 'long', false)); ?></td>
		<td><?php echo $row->amount; ?></td>
	</tr>
<?php }} else {	echo "<b><i>No Records Found.</i></b>";	} ?>
	<!--tr>
		<td><b>Total</b></td>
		<td></td>
	</tr-->
</table>
